package westh.ilib.struts.action.researcher;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.bean.researcher.Article;
import westh.ilib.bean.researcher.Researcher;


/**
 * @author zhaojie
 * 
 */

public class GetResearcherDetailAction extends Action {
	private final static Log log = LogFactory.getLog(GetResearcherDetailAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		DynaActionForm f = (DynaActionForm) form;
//		log.debug("in GetResDetailAction ");
		String researcherId =(String)request.getParameter("researcherId");
//		log.debug("in GetResDetailAction 222");
//		log.debug("in GetResDetailAction researcherId " + researcherId);
		JSONObject inputObj = new JSONObject();
		inputObj.put("researcherId", researcherId);
		String input = inputObj.toString();
		
		Researcher  researcher = null;
		List listArticle = null;
		List listRelatedResearcher = null;
		
		//jSon-String
		String outString = "";
		try {
			MuleClient client = new MuleClient();
			UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getResearcherInfo",input,null);
			outString = (String)umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		String retCode = "";
		String retMsg = "";
		JSONObject outJson=JSONObject.fromObject(outString);
		retCode = outJson.getString("retCode");
		JSONObject jsonOut= JSONObject.fromObject(outJson.getString("result"));
		log.debug("in getDetail outString =" + outString);
		
		log.debug("in getDetail retCode =" + retCode);
		
		if (retCode.equals("1400")) {
			// get researcher info
			researcher = new Researcher();
			researcher.setAuthorId(researcherId);
			researcher.setResName(jsonOut.getString("researcherName"));
			researcher
					.setResHomePage(jsonOut.getString("researcherHomepage"));
			researcher.setResInstitute(jsonOut
					.getString("researcherInstitute"));
		} else if (retCode.equals("1401")) {
			retMsg = outJson.getString("retMsg");
			request.setAttribute("info", retMsg);
			return mapping.findForward("fail");
		}
		log.debug("???????????????before getResDetail getResRscList");
		
		outString = "";
		try {
			log.debug("in getResDetail getResRscList");
			MuleClient client = new MuleClient();
			log.debug("in getResDetail getResRscList");
			UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getResearcherRscList",input,null);
			outString = (String)umo.getPayload();
			//log.debug("getResearcherRscList outString=" + outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		retCode = "";
		retMsg = "";
		
		//the follow code is create test data  ,if you hava real data ,please delete the next line
		//outString ="{\"retCode\":\"1500\",\"retMsg\":\"resRscListSucess\",\"result\":[{\"rscId\":\"rsc1\",\"rscPublishDate\":\"2010-08-03\",\"rscAuthor\":\"zhaojie\",\"rscName\":\"ruleEngine 123\"},{\"rscId\":\"rsc2\",\"rscPublishDate\":\"2010-08-03\",\"rscAuthor\":\"zhaojie\",\"rscName\":\"testDate\"}]}";
		
		
		
		log.debug("getResearcherRscList outString=" + outString);
		jsonOut= JSONObject.fromObject(outString);
				
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("1500")) {
			// get article info
			JSONArray result = jsonOut.getJSONArray("result");
			int size = result.size();
			listArticle = new ArrayList();
			for (int i = 0; i < size; ++i) {
				JSONObject articleObj = result.getJSONObject(i);
				Article art = new Article();
				art.setArticleId(articleObj.getString("rscId"));
				art.setArticleName(articleObj.getString("rscName"));
				art.setPublishDate(articleObj.getString("rscPublishDate"));
				art.setAuthors(articleObj.getString("rscAuthor"));
				listArticle.add(art);
			}
			log.debug("Finish parsing.");
		} else if (retCode.equals("1501")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			return mapping.findForward("fail");
		}
		
		outString = "";
		log.debug("Begin get related author");
		try {
			MuleClient client = new MuleClient();
			UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getRelatedAuthor",input,null);
			outString = (String)umo.getPayload();
			log.debug("getRelatedAuthor outString=" + outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		retCode = "";
		retMsg = "";
		jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("1600")) {
			// get related author info
			JSONArray result = jsonOut.getJSONArray("result");
			int size = result.size();
			listRelatedResearcher = new ArrayList();
			for (int i = 0; i < size; ++i) {
				JSONObject resObj = result.getJSONObject(i);
				Researcher res = new Researcher();
				res.setAuthorId(resObj.getString("researcherId"));
				res.setAuthorName(resObj.getString("researcherName"));
				listRelatedResearcher.add(res);
			}
		} else if (retCode.equals("1601")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			return mapping.findForward("fail");
		}

		request.setAttribute("researcher", researcher);
		request.setAttribute("listArticle", listArticle);
		request.setAttribute("listRelated", listRelatedResearcher);
		return mapping.findForward("succ");		
	}

}
